package Euler50;

import java.util.*;
import ReusableCode.*;

public class PrimeSums {

	public static void main(String[] args) {
		int upperbound = 1000000;
		List<Integer> primes = PrimeFunctions.GetPrimes(upperbound);
		
		System.out.println("Working...");
		
		long sum = 0;
		int start = 1000;
		for(int i = 1; i < primes.size(); i++)
		{
			sum += primes.get(i);
			if(sum>upperbound)
			{
				start = i;
				sum = 0;
				break;
			}
		}
		
		for(int i = start; i > 0; i--)
		{
			List<List<Integer>> sub = getSubsequences(primes, i, upperbound);
			boolean found = false;
			
			for(int j = 0; j < sub.size(); j++)
			{
				sum = MathFunctions.getSetSum(sub.get(j));
				
				if(PrimeFunctions.isPrime(sum) && (sum < upperbound))
				{
					System.out.println(sum);
					System.out.println(sub.get(j));
					System.out.println(sub.get(j).size());
					found = true;
				}
			}
			if(found)
				break;
		}
		
	}
	
	
	public static List<List<Integer>> getSubsequences(List<Integer> list, int length, int ubound)
	{
		List<List<Integer>> subsequences = new ArrayList<List<Integer>>();
		
		for(int i = 0; i < list.size()-length; i++)
		{
			List<Integer> sequence = new ArrayList<Integer>();
			long sum = 0;
			
			for(int j = i; j < i + length; j++)
			{
				sum += list.get(j);
				sequence.add(list.get(j));
				
				if(sum>ubound)
				{
					break;
				}
			}
			if(sum<ubound)
			{
				subsequences.add(sequence);
			}
			
		}
		
		return subsequences;
	}
	
	
}
